package com.ruoyi.web.teacher;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.teacher.vo.TQuestionBankVo;
import com.ruoyi.teacher.service.ITQuestionBankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 题库Controller
 *
 * @author ruoyi
 * @date 2024-12-03
 */
@Api(tags = "题库Controller")
@RestController
@RequestMapping("/teacher/bank")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TQuestionBankController extends BaseController {

    private final ITQuestionBankService tQuestionBankService;

    @ApiOperation("查询题库列表")
    @PreAuthorize("@ss.hasPermi('teacher:bank:list')")
    @GetMapping("/list")
    public TableDataInfo<TQuestionBankVo> list(TQuestionBankVo entity) {

        return tQuestionBankService.queryList(entity);
    }

    @ApiOperation("查询题库所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(TQuestionBankVo entity) {
        return AjaxResult.success("查询成功", tQuestionBankService.queryAll(entity));
    }

    @ApiOperation("导出题库列表")
    @PreAuthorize("@ss.hasPermi('teacher:bank:export')")
    @Log(title = "题库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TQuestionBankVo entity) {
        List<TQuestionBankVo> list = tQuestionBankService.queryAll(entity);
        ExcelUtil<TQuestionBankVo> util = new ExcelUtil<>(TQuestionBankVo.class);
        util.exportExcel(response, list, "题库数据");
    }

    @ApiOperation("获取题库详细信息")
    @PreAuthorize("@ss.hasPermi('teacher:bank:query')")
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", tQuestionBankService.queryById(id));
    }

    @ApiOperation("新增题库")
    @PreAuthorize("@ss.hasPermi('teacher:bank:add')")
    @Log(title = "题库", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody TQuestionBankVo entity) {
        return toAjax(tQuestionBankService.save(entity));
    }

    @ApiOperation("修改题库")
    @PreAuthorize("@ss.hasPermi('teacher:bank:edit')")
    @Log(title = "题库", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody TQuestionBankVo entity) {
        return toAjax(tQuestionBankService.updateById(entity));
    }

    @ApiOperation("删除题库")
    @PreAuthorize("@ss.hasPermi('teacher:bank:remove')")
    @Log(title = "题库", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tQuestionBankService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
